//
// Created by qinshizhong on 6/30/20.
//

#ifndef ZM32_GW_UNIDEV_H
#define ZM32_GW_UNIDEV_H

#include "cJSON.h"
#include "zm32.h"

#define UNIDEV_SUCCESS              (0)
#define UNIDEV_ERR_NULL             (-1)
#define UNIDEV_ERR_PDU              (-2)
#define UNIDEV_ERR_UUID             (-3)
#define UNIDEV_ERR_OP               (-4)
#define UNIDEV_ERR_ATTRID           (-5)
#define UNIDEV_ERR_CHANNEL          (-6)
#define UNIDEV_ERR_VALUE            (-7)
#define UNIDEV_ERR_LIB              (-8)
#define UNIDEV_ERR_MEM              (-9)
#define UNIDEV_ERR_UNKNOWN_DEV      (-10)
#define UNIDEV_ERR_MODEL            (-11)
#define UNIDEV_ERR_CNF              (-12)
#define UNIDEV_ERR_TYPE             (-13)
#define UNIDEV_ERR_UNKNOWN          (~0UL)

#define RW_RETRY_TIMES              (2)

#define TYPE_STR_NAME               "string"
#define TYPE_INT_NAME               "integer"
#define TYPE_BOOL_NAME              "boolean"
#define TYPE_FLOAT_NAME             "float"
#define TYPE_JSON_NAME              "json"
#define TYPE_RAW_NAME               "raw"
#define TYPE_UNKNOWN_NAME           "unknown"


#define MAX_RULES                   (5)
#define MAX_ACTS                    (MAX_RULES)
#define UNIDEV_EQ                   (0)
#define UNIDEV_NEQ                  (1)
#define UNIDEV_GT                   (2)
#define UNIDEV_GE                   (3)
#define UNIDEV_LT                   (4)
#define UNIDEV_LE                   (5)



typedef  void (*dev_report)(const cJSON *result);
typedef void (*unidev_report)(const char *result);


struct dev_cb {
    dev_report attr;
    dev_report join;
    dev_report quit;
};



int init(dev_report attr, dev_report join, dev_report quit, void *par);
int finit(void);
int rdwr_sync(const cJSON *request, char *result);
int rdwr_async(const cJSON *request, unidev_report cb);
int networking_start(unsigned short dur);
int networking_stop(void);
int dev_rm(const cJSON *request, char *result);
int model_update(void *par);

const struct regs * regs_lookup(const struct attr *attr);
const struct attr * attr_lookup(const struct regs *regs, struct regs *next);
const struct attr_reg_map * attr_reg_map_lookup(const struct attr *attr,
                                                const struct regs *regs);

#endif //ZM32_GW_UNIDEV_H
